// 用户管理页面
const app = getApp();
const request = require('../../../utils/request.js').request;

Page({
  data: {
    loading: true,
    users: [],
    showEditForm: false,
    currentUser: null,
    formData: {
      id: '',
      username: '',
      phone: '',
      role: 1,
      status: 1,
      password: ''
    }
  },

  onLoad: function (options) {
    this.loadUsers();
  },

  onPullDownRefresh: function () {
    this.loadUsers();
  },

  // 加载用户列表
  loadUsers: function () {
    this.setData({ loading: true });
    
    request({
      url: '/api/user/all',
      method: 'GET',
      success: (res) => {
        if (res.code === 0) {
          this.setData({
            users: res.data,
            loading: false
          });
        } else {
          wx.showToast({
            title: res.msg || '获取用户列表失败',
            icon: 'none'
          });
          this.setData({ loading: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        this.setData({ loading: false });
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },

  // 显示编辑表单
  showEditForm: function (e) {
    const index = e.currentTarget.dataset.index;
    const user = this.data.users[index];
    
    this.setData({
      showEditForm: true,
      currentUser: user,
      formData: {
        id: user.id,
        username: user.username,
        phone: user.phone || '',
        role: user.role,
        status: user.status,
        password: ''
      }
    });
  },

  // 关闭表单
  closeForm: function () {
    this.setData({
      showEditForm: false
    });
  },

  // 输入表单数据
  inputChange: function (e) {
    const { field } = e.currentTarget.dataset;
    let { formData } = this.data;
    formData[field] = e.detail.value;
    this.setData({ formData });
  },

  // 提交编辑表单
  submitEditForm: function () {
    const { formData } = this.data;
    
    request({
      url: `/api/user/${formData.id}`,
      method: 'PUT',
      data: formData,
      success: (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '更新成功',
            icon: 'success'
          });
          this.setData({ showEditForm: false });
          this.loadUsers();
        } else {
          wx.showToast({
            title: res.msg || '更新失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  // 禁用用户
  disableUser: function (e) {
    const index = e.currentTarget.dataset.index;
    const user = this.data.users[index];
    
    wx.showModal({
      title: '确认禁用',
      content: `确定要禁用用户"${user.username}"吗？`,
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/user/${user.id}/disable`,
            method: 'PUT',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '禁用成功',
                  icon: 'success'
                });
                this.loadUsers();
              } else {
                wx.showToast({
                  title: res.msg || '禁用失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 启用用户
  enableUser: function (e) {
    const index = e.currentTarget.dataset.index;
    const user = this.data.users[index];
    
    wx.showModal({
      title: '确认启用',
      content: `确定要启用用户"${user.username}"吗？`,
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/user/${user.id}/enable`,
            method: 'PUT',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '启用成功',
                  icon: 'success'
                });
                this.loadUsers();
              } else {
                wx.showToast({
                  title: res.msg || '启用失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 删除用户
  deleteUser: function (e) {
    const index = e.currentTarget.dataset.index;
    const user = this.data.users[index];
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除用户"${user.username}"吗？该操作不可恢复！`,
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/user/${user.id}`,
            method: 'DELETE',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                this.loadUsers();
              } else {
                wx.showToast({
                  title: res.msg || '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  }
}) 